/*
 * Project Name: cms_web
 * File Name: ArticleAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月17日 上午9:09:37
 */
package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.action.ClassesIndexAction;
import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.ArticleEntity;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.ArticleService;
import com.shishuo.cms.service.ClassesService;

/**
 * 
 * @since 2015年3月17日 上午9:09:37
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleAction extends AdminBaseAction {
	@Autowired
	private ArticleService articleService;

	@Autowired
	private ClassesIndexAction classesIndexAction;

	@InitBinder("articleEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("articleEntity.");
	}

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addUser(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("article");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/article/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public JsonVo<String> addNewClasses(
			@ModelAttribute ArticleEntity articleEntity,
			@RequestParam(value = "content", defaultValue = "") String content)
			throws Exception {
		articleEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (articleEntity.getTitle() == null
				|| "".equals(articleEntity.getTitle())) {
			json.getErrors().put("title", "标题不能为空");
		}
		if (articleEntity.getContent() == null
				|| "".equals(articleEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (articleEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		articleService.insert(articleEntity);
		json.setResult(true);
		return json;
	}

	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("p", pageNum);
		modelMap.put("pageVo", articleService.getAllListPage(pageNum));
		return "system/article/list";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delete(
			@RequestParam(value = "articleId") long articleId) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		articleService.deleteById(articleId);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "articleId") long articleId,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		ArticleEntity articleEntity=articleService.getById(articleId);
		modelMap.put("articleEntity", articleEntity);
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("article");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/article/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
			@ModelAttribute ArticleEntity articleEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (articleEntity.getTitle() == null
				|| "".equals(articleEntity.getTitle())) {
			json.getErrors().put("title", "标题不能为空");
		}
		if (articleEntity.getContent() == null
				|| "".equals(articleEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (articleEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		ArticleEntity oldArticleEntity=articleService.getById(articleEntity.getArticleId());
		if (null==oldArticleEntity) {
			json.getErrors().put("title", "文章不存在");
			return json;
		}
		oldArticleEntity.setClassId(articleEntity.getClassId());
		oldArticleEntity.setContent(content);
		oldArticleEntity.setUpdateTime(new Date());
		oldArticleEntity.setTitle(articleEntity.getTitle());
		articleService.update(oldArticleEntity);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/preview.htm", method = RequestMethod.GET)
	public String preview(@RequestParam(value = "articleId") long articleId,
			@RequestParam(value = "p", defaultValue = "0") int p,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		return classesIndexAction.articleItemDefault(articleId, request,
				modelMap);
	}
}
